package com.sky.loveshop.controller;

import cn.hutool.json.JSONUtil;
import com.sky.loveshop.api.MeiTuanOrderRequest;
import com.sky.loveshop.entity.MeiTuanOrder;
import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.common.PageResult;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.meiTuan.*;
import com.sky.loveshop.entity.qo.MtOrderInfoQO;
import com.sky.loveshop.entity.vo.MeiTuanOrderVO;
import com.sky.loveshop.entity.vo.SkuVO;
import com.sky.loveshop.service.MtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/9/2 23:13
 */
@Slf4j
@RestController
@RequestMapping("/meiTuan")
public class MeiTuanController {

    @Autowired
    private MtService mtService;

    /**
     * 创建门店
     */
    @GetMapping("/createStore")
    public Result<MtStoreCreateResponse> createStore() {
        MtStoreCreateResponse store = mtService.createStore();
        return Result.success(store);
    }

    /**
     * 创建美团订单
     */
    @PostMapping("/createMtOrder")
    public Result<MtOrderCreateResponse> createMtOrder(@RequestBody MeiTuanOrderRequest.CreateOrderRequest request) {
        log.info("createMtOrderParam = {}", JSONUtil.toJsonStr(request));
        MtOrderCreateResponse mtOrder = mtService.createMtOrder(request);
        return Result.success(mtOrder);
    }

    /**
     * 主动拉取美团订单 把结果返回 问题排查时使用
     */
    @GetMapping("/pullMtOrder")
    public Result getMtOrderAndInsert(Integer storeId, Integer deliverStatus) {
        //storeId = 168248; //光明店
        //storeId = 249981; //光明店
        if (null == storeId) {
            return Result.ofError(10001, "门店id不能为空");
        }
        //mtService.getMtOrderAndInsert(storeId);
        MtOrderPullResponse response = mtService.getMtOrderInfoListByStoreId(storeId, deliverStatus);
        return Result.success(response);
    }

    /**
     * 获取当前门店的订单
     */
    @PostMapping("/getMtOrderList")
    public Result<List<MtOrder>> getMtOrderList(@RequestBody MtOrderInfoQO qo) {
        log.info("getMtOrderListParam = {}", JSONUtil.toJsonStr(qo));
        return Result.success(mtService.getMtOrderList(qo));
    }

    /**
     * 获取当前门店的订单分页(现场核销APP使用)
     * 入参：shopId我方商店id
     */
    @PostMapping("/listMtOrderPages")
    public Result<PageResult<MeiTuanOrder>> listMtOrderPages(@RequestBody MtOrderInfoQO qo) {
        log.info("listMtOrderPagesPram={}", JSONUtil.toJsonStr(qo));
        return Result.page(mtService.listMtOrderPages(qo));
    }

    /**
     * 获取所有门店的美团订单分页(管理后台,库存管理APP使用)
     */
    @PostMapping("/listAllMtOrderPages")
    public Result<PageResult<MeiTuanOrderVO>> listAllMtOrderPages(@RequestBody MtOrderInfoQO qo) {
        log.info("listAllMtOrderPagesParam = {}", JSONUtil.toJsonStr(qo));
        return Result.page(mtService.listAllMtOrderPages(qo));
    }

    /**
     * 根据id获取美团订单详情单
     */
    @GetMapping("/getMtOrderDetailsById")
    public Result<List<MtOrderDetail>> getMtOrderDetailsById(Integer id) {
        log.info("getMtOrderDetailsByIdParam = {}", id);
        return Result.success(mtService.getMtOrderDetailsById(id));
    }

    /**
     * 根据商品条形码和商店id获取商品详情(扫码器扫描条形码获取商品详情)
     * 返回关键信息：upcCode（upcCode跟商品条形码是一一对应的关系）
     */
    @GetMapping("/getSkuDetailByBarCode")
    public Result<SkuVO> getSkuDetailByBarCode(String barCode, Integer shopId) throws BizException {
        log.info("getSkuDetailByBarCodeParam 条形码={} 商店id={}", barCode, shopId);
        return Result.success(mtService.getSkuDetailByBarCode(barCode, shopId));
    }

    /**
     * 根据订单id进行核销订单
     */
    @GetMapping("/dealMtOrder")
    public Result<Integer> dealMtOrder(Integer id) {
        log.info("dealMtOrderParam  订单id={}", id);
        return Result.success(mtService.dealMtOrder(id));
    }

    /**
     * 标记美团订单为刷单
     */
    @GetMapping("/markBrushFlag")
    public Result<Integer> markBrushFlag(Integer id) {
        log.info("markBrushFlag 将订单标记为刷单 订单id={}", id);
        return Result.success(mtService.markBrushFlag(id));
    }

    /**
     * 手动调用外送帮接口获取某个订单的配送信息
     */
    @GetMapping("/pullDeliverRecordsByPlatformOrderId")
    public Result<MtOrderPullDeliverRecordsResponse> pullDeliverRecordsByPlatformOrderId(String platformOrderId) {
        log.info("pullDeliverRecordsByPlatformOrderId 平台订单id={}", platformOrderId);
        return Result.success(mtService.pullDeliverRecordsByPlatformOrderId(platformOrderId));
    }


}
